
using System.Collections;
using System.Collections.Generic;
using TransformControlUser;
using UnityEngine;
using static TransformControlUser.TransformControl;

public class TransformManager : MonoBehaviour
{
    public TransformControl transformControl;
    public GameObject Target;
    public TransformMode mode;
    RaycastHit hit;
    void Start()
    {
        transformControl.DragEvent.AddListener(open);
    }

    // Update is called once per frame
    void Update()
    {
        if (Target)
        {
            transformControl.Control();
        }
        if (Input.GetMouseButton(1))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (Target)
                {
                    ExitHigh(Target);
                }
                Target = hit.collider.gameObject;
                transformControl.TargetObjct = Target.transform;
                transformControl.mode = mode;
                HighObj(Target);
            }
        }
        if (Input.GetKey(KeyCode.W))
        {
            mode = TransformMode.Translate;
            transformControl.mode = mode;
        }
        if (Input.GetKey(KeyCode.R))
        {
            mode = TransformMode.Rotate;
            transformControl.mode = mode;
        }
        if (Input.GetKey(KeyCode.S))
        {
            mode = TransformMode.Scale;
            transformControl.mode = mode;
        }

    }
    void ExitHigh(GameObject target)
    {
        HighlightableObject highlightableObject;
        target.TryGetComponent<HighlightableObject>(out highlightableObject);
        highlightableObject.ConstantOffImmediate();
    }
    void HighObj(GameObject target)
    {
        HighlightableObject highlightableObject;
        target.TryGetComponent<HighlightableObject>(out highlightableObject);
        if (highlightableObject)
        {
            highlightableObject.ConstantParams(Color.yellow);
            highlightableObject.ConstantOn();
        }
        else
        {
            target.AddComponent<HighlightableObject>();
            HighObj(target);
        }
    }
    void open()
    {
        Debug.Log("as");
    }


}
